package com.crx.springbootinit.controller.admin;

import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crx.springbootinit.common.BaseResponse;
import com.crx.springbootinit.model.entity.Post;
import com.crx.springbootinit.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/echarts")
@Api(tags = "后台图表模块接口")
public class EchartsAdminController {
    @Autowired
    private IPostService postService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ILoveService loveService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private ICollectService collectService;

    @ApiOperation("获取后台统计数据")
    @GetMapping("/dashboardData")
    public BaseResponse<Map<String, Object>> getDashboardData() {
        // 假设通过服务层获取数据
        Long postCount = postService.count();
        Long userCount = userService.count();
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        // 设置时间条件为今日00：00到23：59之间
        LocalDateTime min = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime max = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        queryWrapper.ge(Post::getCreatedTime,min);
        queryWrapper.le(Post::getCreatedTime,max);
        Long newPostsToday = postService.count(queryWrapper);

        // 返回合并数据
        Map<String, Long> data = new HashMap<>();
        data.put("postCount", postCount);
        data.put("userCount", userCount);
        data.put("newPostsToday", newPostsToday);

        return BaseResponse.success(data);
    }

    @ApiOperation("获取用户增长趋势")
    @GetMapping("/userTrend")
    public BaseResponse<Map<String, Object>> getUserTrend() {
        List<HashMap<String, Object>> data = new ArrayList<>();
        data = userService.getUserTrend();
        return BaseResponse.success(data);
    }

    @ApiOperation("获取帖子发布情况")
    @GetMapping("/postTrend")
    public BaseResponse<Map<String, Object>> getPostTrend() {
        List<HashMap<String, Object>> data = new ArrayList<>();
        data = postService.getPostTrend();
        return BaseResponse.success(data);
    }

    @ApiOperation("获取用户年龄分组")
    @GetMapping("/userAgeGroup")
    public BaseResponse<Map<String, Object>> getUserAgeGroup() {
        List<HashMap<String, Object>> data = new ArrayList<>();
        data = userService.getUserAgeGroup();
        return BaseResponse.success(data);
    }

    @ApiOperation("获取活跃指标")
    @GetMapping("/activeIndicators")
    public BaseResponse<Map<String, Object>> getActiveIndicators() {
        Map<String, Object> data = new HashMap<>();

        Long postCount = postService.count();
        Long collectCount = collectService.count();
        Long loveCount = loveService.count();
        Long commentCount = commentService.count();

        data.put("postCount", postCount);
        data.put("collectCount", collectCount);
        data.put("loveCount", loveCount);
        data.put("commentCount", commentCount);

        return BaseResponse.success(data);
    }
}
